<template>
  <div class="app-container">
    <el-row style="margin-bottom: 10px">
      <el-col :span="7">
        <el-date-picker
          v-model="dateList"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="getList"
        >
        </el-date-picker>
      </el-col>
      <el-col :span="5">
        <el-button
          type="primary"
          icon="el-icon-arrow-down"
          size="mini"
          style="margin-left: 10px"
          @click="handleShow"
        > </el-button>
      </el-col>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="分销商ID" prop="resellerUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.resellerUserId"-->
<!--          placeholder="请输入分销商ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="支付金额" prop="payMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.payMoney"-->
<!--          placeholder="请输入支付金额"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="分润比例" prop="ratio">-->
<!--        <el-input-->
<!--          v-model="queryParams.ratio"-->
<!--          placeholder="请输入分润比例"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="佣金" prop="money">-->
<!--        <el-input-->
<!--          v-model="queryParams.money"-->
<!--          placeholder="请输入佣金"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="佣金类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择">
          <el-option
            v-for="item in typeList"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择">
          <el-option
            v-for="item in orderTypeList"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="货币" prop="currency">
        <el-select v-model="queryParams.currency" placeholder="请选择">
          <el-option
            v-for="item in langList"
            :key="item.id"
            :label="item.currency"
            :value="item.currency">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="积分兑换比例" prop="exchangeRate">-->
<!--        <el-input-->
<!--          v-model="queryParams.exchangeRate"-->
<!--          placeholder="请输入积分兑换比例"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="积分佣金" prop="totalMoney">-->
<!--        <el-input-->
<!--          v-model="queryParams.totalMoney"-->
<!--          placeholder="请输入积分佣金"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备注" prop="memo">-->
<!--        <el-input-->
<!--          v-model="queryParams.memo"-->
<!--          placeholder="请输入备注"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="创建时间" prop="createtime">-->
<!--        <el-input-->
<!--          v-model="queryParams.createtime"-->
<!--          placeholder="请输入创建时间"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-descriptions direction="vertical" :column="5" border style="font-weight: bold;font-size: 16px;margin-bottom: 10px">
      <el-descriptions-item label="佣金总积分">{{ totalMap.totalMoney }}</el-descriptions-item>
      <el-descriptions-item label="总记录数量">{{ totalMap.totalCount }}</el-descriptions-item>
      <el-descriptions-item label="付款总金额">{{ totalMap.payAmount }}</el-descriptions-item>
      <el-descriptions-item label="佣金总金额">{{ totalMap.moneyAmount }}</el-descriptions-item>
    </el-descriptions>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="${comment}" align="center" prop="siteId" />-->
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 'direct'">直接佣金</span>
          <span v-if="scope.row.type == 'indirect'">间接佣金</span>
        </template>
      </el-table-column>
      <el-table-column label="分销商ID" align="center" prop="resellerUserId" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="支付金额" align="center" prop="payMoney" />
      <el-table-column label="分润比例" align="center" prop="ratio" />
      <el-table-column label="佣金" align="center" prop="money" />
      <el-table-column label="货币标准符号" align="center" prop="currency" />
      <el-table-column label="积分兑换比例" align="center" prop="exchangeRate" />
      <el-table-column label="积分佣金" align="center" prop="totalMoney" />
<!--      <el-table-column label="备注" align="center" prop="memo" />-->
      <el-table-column label="订单类型" align="center" prop="orderType" >
        <template slot-scope="scope">
          <span v-if="scope.row.type == 'vip'">VIP订单</span>
          <span v-if="scope.row.type == 'reseller'">分销商订单</span>
          <span v-if="scope.row.type == 'usable'">剧场积分订单</span>
        </template>
      </el-table-column>
      <el-table-column label="订单ID" align="center" prop="orderId" />
<!--      <el-table-column label="创建时间" align="center" prop="createtime" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改分佣记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="分销商ID" prop="resellerUserId">
          <el-input v-model="form.resellerUserId" placeholder="请输入分销商ID" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="支付金额" prop="payMoney">
          <el-input v-model="form.payMoney" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="分润比例" prop="ratio">
          <el-input v-model="form.ratio" placeholder="请输入分润比例" />
        </el-form-item>
        <el-form-item label="佣金" prop="money">
          <el-input v-model="form.money" placeholder="请输入佣金" />
        </el-form-item>
        <el-form-item label="货币标准符号" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入货币标准符号" />
        </el-form-item>
        <el-form-item label="积分兑换比例" prop="exchangeRate">
          <el-input v-model="form.exchangeRate" placeholder="请输入积分兑换比例" />
        </el-form-item>
        <el-form-item label="积分佣金" prop="totalMoney">
          <el-input v-model="form.totalMoney" placeholder="请输入积分佣金" />
        </el-form-item>
        <el-form-item label="备注" prop="memo">
          <el-input v-model="form.memo" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog, selectCount } from "@/api/reseller/log";
import {listLang} from "@/api/video/lang";

export default {
  name: "Log",
  data() {
    return {
      totalMap: {},
      langList: [],
      dateList: [],
      typeList: [
        { value: 'direct', label: '直接佣金' },
        { value: 'indirect', label: '间接佣金' }
      ],
      orderTypeList: [
        { value: 'vip', label: 'VIP订单' },
        { value: 'reseller', label: '分销商订单' },
        { value: 'usable', label: '剧场积分订单' }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 分佣记录表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteId: null,
        type: null,
        resellerUserId: null,
        userId: null,
        payMoney: null,
        ratio: null,
        money: null,
        currency: null,
        exchangeRate: null,
        totalMoney: null,
        memo: null,
        orderType: null,
        orderId: null,
        createtime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteId: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型:direct=直接佣金,indirect=间接佣金不能为空", trigger: "change" }
        ],
        resellerUserId: [
          { required: true, message: "分销商ID不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        payMoney: [
          { required: true, message: "支付金额不能为空", trigger: "blur" }
        ],
        ratio: [
          { required: true, message: "分润比例不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "佣金不能为空", trigger: "blur" }
        ],
        currency: [
          { required: true, message: "货币标准符号不能为空", trigger: "blur" }
        ],
        exchangeRate: [
          { required: true, message: "积分兑换比例不能为空", trigger: "blur" }
        ],
        totalMoney: [
          { required: true, message: "积分佣金不能为空", trigger: "blur" }
        ],
        memo: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型:vip=VIP订单,reseller=分销商订单,usable=剧场积分订单不能为空", trigger: "change" }
        ],
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
        createtime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTotalMap();
    this.getLangList();
  },
  methods: {
    getTotalMap() {
      this.loading = true;
      selectCount(this.queryParams).then(response => {
        this.totalMap = response.data;
        this.loading = false;
      });
    },
    getLangList() {
      this.loading = true;
      listLang().then(response => {
        this.langList = response.rows;
        this.loading = false;
      });
    },
    /** 隐藏/展示查询列表 */
    handleShow() {
      this.showSearch = !this.showSearch;
    },
    /** 查询分佣记录列表 */
    getList() {
      this.loading = true;
      if(Object.keys(this.dateList).length > 0) {
        this.queryParams.beginPaytime = this.dateList[0].getTime()
        this.queryParams.endPaytime = this.dateList[1].getTime()
      }
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteId: null,
        type: null,
        resellerUserId: null,
        userId: null,
        payMoney: null,
        ratio: null,
        money: null,
        currency: null,
        exchangeRate: null,
        totalMoney: null,
        memo: null,
        orderType: null,
        orderId: null,
        createtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加分佣记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改分佣记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLog(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除分佣记录编号为"' + ids + '"的数据项？').then(function() {
        return delLog(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/log/export', {
        ...this.queryParams
      }, `log_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
